package com.seaengine.util
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class BitmapDataUtil
	{
		public function BitmapDataUtil()
		{
		}
		
		/**
		 * 土办法使图片变灰
		 * @param bitmapData
		 * @return 
		 * 
		 */
		public static function getGray(bitmapData:BitmapData):BitmapData
		{
			var R:uint,G:uint,B:uint,color:uint,gray:uint;
			var rslt:BitmapData = bitmapData.clone();//new BitmapData(bitmapData.width,bitmapData.height,bitmapData.transparent);
			for (var i:int = 0, lenX:int = bitmapData.width; i < lenX; i++)
				for (var j:int = 0, lenY:int = bitmapData.height; j < lenY; j++)
				{
					color = bitmapData.getPixel(i, j);
					R = (color >> 16) & (0xff);
					G = (color >> 8) & (0xff);
					B = (color >> 0) & (0xff);
					R = (R+G+B)/3;
					gray = R + (R << 8) + (R << 16);
					rslt.setPixel(i, j, gray);
				}
			return rslt;
		}
		
		public static function realHitTest(object:DisplayObject, point:Point):Boolean 
		{
            if(object is BitmapData) {
                return (object as BitmapData).hitTest(new Point(0,0), 0, object.globalToLocal(point));
            }
            else {
                if(!object.hitTestPoint(point.x, point.y)) {
                    return false;
                }
                else {
                    /* So now we make a new BitmapData object and draw the pixels of our object
                     * in there. Then we use the hitTest method of that BitmapData object to
                     * really find out of we have a hit or not.
                     */
                    var bmapData:BitmapData = new BitmapData(object.width, object.height, true, 0x00000000);
                    bmapData.draw(object, new Matrix());
                    
                    var returnVal:Boolean = bmapData.hitTest(new Point(0,0), 0, object.globalToLocal(point));
                    
                    bmapData.dispose();
                    
                    return returnVal;
                }
            }
        }

	}
}